<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
var replaceSpace = function(s) {
    return s.replace(/ /g,'%20')  // 可以正则替换所有空格 replace(/\_/g,"-") 记得转义
};

var s = "We are happy."
console.log(replaceSpace(s))
    </script>
</body>
</html>